using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using AllIsWell.Core.Models;

namespace AllIsWell.Persistance.Ef
{
    public class MyDiaryDbInitStratergy : DropCreateDatabaseIfModelChanges<MyDiary>
    {
        protected override void Seed(MyDiary myDiary)
        {
            CreateUniqueConstraintOnPageModel(myDiary);
            PouplateDbWithPredefinedDestinies(myDiary);
            base.Seed(myDiary);
        }

        private void CreateUniqueConstraintOnPageModel(MyDiary myDiary)
        {
            // Reference http://bit.ly/mklmTo
            myDiary.Database.ExecuteSqlCommand("ALTER TABLE Pages ADD CONSTRAINT uc_date_destiny UNIQUE (DateTime, Destiny_Id)");
        }

        private void PouplateDbWithPredefinedDestinies(MyDiary myDiary)
        {
            var startItWell = GetDestiny("Start It Well", "Set the tone for the day", 
                                            "You've successfully set the tone for the day. Wish you a great day");
            var finishItStrong = GetDestiny("Finish It Strong", "Signoff the day with a sense of accomplishment", 
                                                "Hope you had a great day and All is Well. Good night. Sweet dreams");

            PopulateStartItWellWithPredefinedWhats(startItWell);
            PopulateFinishItStrongWithPredefinedWhats(finishItStrong);
            myDiary.Destinies.Add(startItWell);
            myDiary.Destinies.Add(finishItStrong);
        }

        private Destiny GetDestiny(string path, string description, string greetingMessage)
        {
            return new Destiny
                       {
                           Path = path, 
                           Description = description ,
                           GreetingMessage = greetingMessage,
                           Whats = new List<What>()
                       };
        }

        private void PopulateStartItWellWithPredefinedWhats(Destiny startItWell)
        {
            var startItWellQuestions = new[]
            {
                "What are you going to do today for your organisation ?",
                "What are you going to do today for your family ?",
                "What are you going to do today for your career ?",
                "What are you going to do today for your society ?",
                "What are you going to do today for yourself ?"
            };

            var whats = startItWellQuestions.Select(question => GetWhat(startItWell, question));
            startItWell.Whats.AddRange(whats);    
        }

        private static What GetWhat(Destiny destiny, string question)
        {
            return new What {Question = question, Destiny = destiny, Enabled = true };
        }

        private void PopulateFinishItStrongWithPredefinedWhats(Destiny finishItStrong)
        {
            var finishItStrongQuestions = new[]
            {
                "What went right today ?",
                "What went wrong today ?",
                "What have you learnt today ?",
                "What is the tip for the day?"
            };

            var whats = finishItStrongQuestions.Select(question => GetWhat(finishItStrong, question));
            finishItStrong.Whats.AddRange(whats);    
        }
    }
}